# 1.导包
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from faker import Faker

fk = Faker("zh_CN")

# 2.打开浏览器（创建浏览器驱动对象）
path = r"C:\Program Files\Python311\chromedriver.exe"
ser = Service(executable_path=path)  # Chrome浏览器驱动服务对象
driver = webdriver.Chrome(service=ser)  # 打开Chrome浏览器
driver.maximize_window()
# 3.输入网址
driver.get("http://121.43.169.97:8081/common/member/login")
# 4.页面操作
# 登录
driver.find_element(By.ID, "keywords").send_keys("17912573189")
driver.find_element(By.ID, "password").send_keys("123456a")
driver.find_element(By.ID, "login-btn").click()
# 点击开通资金托管
# presence_of_element_located()：只要该元素存在就可以获取元素
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.LINK_TEXT, "立即开通"))).click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "realname"))).send_keys(fk.name())
driver.find_element(By.NAME, "card_id").send_keys(fk.ssn())
driver.find_element(By.CSS_SELECTOR, "[value='确认提交']").click()
# 点击立即开通
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".btn.ng-scope"))).click()
# 获取新窗口
time.sleep(2)
handles = driver.window_handles
driver.switch_to.window(handles[1])
# visibility_of_element_located()：元素不但要存在而且要可见，才能获取元素
result = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body"))).text
assert "UserRegister OK" in result
# 5.等待2秒
time.sleep(2)
# 6.退出浏览器
driver.quit()
